package com.zhaojonhy.zhaopangwb.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.zhaojonhy.zhaopangwb.R;
import com.zhaojonhy.zhaopangwb.adapter.ImageBrowserAdapter;
import com.zhaojonhy.zhaopangwb.bean.Statuses;
import com.zhaojonhy.zhaopangwb.bean.PicUrls;
import com.zhaojonhy.zhaopangwb.utils.ImageUtils;
import com.zhaojonhy.zhaopangwb.utils.ToastUtils;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by Administrator on 2016/10/19.
 */

public class ImageBrowserActivity extends Activity implements View.OnClickListener{

    private static final String TAG = ImageBrowserAdapter.class.getSimpleName() ;

    private TextView tv_image_index ;
    private ViewPager vp_image_browser ;
    private Button btn_save ;
    private Button btn_original_image ;
    //获取传递的数据
    private Statuses status ;
    private int position ;
    //创建图片地址的集合
    private ArrayList<PicUrls> imgUrls ;
    //创建viewPage适配器
    private ImageBrowserAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_browser);
        //传入数据初始化
        initData() ;
        //初始化控件
        initView() ;
        //设置数据到adapter
        setData() ;
    }

    private void initData() {
        status = (Statuses)getIntent().getSerializableExtra("status") ;
        //当获取int类型数据无法获取时，返回后面的值
        position = getIntent().getIntExtra("position",0) ;
        Log.d(TAG,"position-->" +position ) ;
        imgUrls = status.getPic_urls() ;
    }

    private void initView() {
        tv_image_index = (TextView) findViewById(R.id.tv_image_index) ;
        vp_image_browser = (ViewPager) findViewById(R.id.vp_image_browser) ;
        btn_save = (Button) findViewById(R.id.btn_save) ;
        btn_original_image = (Button) findViewById(R.id.btn_original_image) ;

        btn_save.setOnClickListener(this);
        btn_original_image.setOnClickListener(this);
    }

    private void setData() {
        adapter = new ImageBrowserAdapter(this,imgUrls) ;
        vp_image_browser.setAdapter(adapter);

        //滑动到当前位置,因为无限轮播所以要去int的最大值中间插入，这样开始就能左右滑动
        final int size = imgUrls.size() ;
        /*
        * int initPosition = 100 / 2 / 9 * 9 = 45
        */
        int midPosition = Integer.MAX_VALUE / 2 / size * size ; //除以size再乘以size 这样就能得到size整数倍的中间值了
        int initPosition = midPosition + position ;
        if( size > 1 ){
            tv_image_index.setVisibility(View.VISIBLE);
            tv_image_index.setText( ( position + 1 ) +"/" + size );
        }else {
            tv_image_index.setVisibility(View.GONE);
        }
        vp_image_browser.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) { //位置变化

            }

            @Override
            public void onPageSelected(int i) { //选中了哪一页
                //无限轮播后，position值很大，处理下
                int index = i % size ;
                tv_image_index.setText((index+1) + "/" + size);
                PicUrls picUrls = adapter.getPic(i) ;
                //如果显示过原图了，隐藏原图按钮
                btn_original_image.setVisibility(picUrls.isShowOriImg() ?
                        View.GONE : View.VISIBLE);

            }

            @Override
            public void onPageScrollStateChanged(int i) { //状态变化

            }
        });
        vp_image_browser.setCurrentItem(initPosition);
    }

    @Override
    public void onClick(View v) {
        //获取viewPager的当前位置，因为是无限轮播，所以很大，要在适配器中换算
        PicUrls picUrls = adapter.getPic(vp_image_browser.getCurrentItem()) ;
        Log.d(TAG,"v.getId()-->" + v.getId() + "   " + "R.id.btn_save-->" + R.id.btn_save ) ;
        switch (v.getId()) {
            case R.id.btn_save:
                Log.d(TAG,"btn_save-->" + "保存图片" ) ;
                Bitmap bitmap = adapter.getBitmap(vp_image_browser.getCurrentItem()) ;
                Log.d(TAG,"bitmap-->" + "获取bitmap" ) ;
                //图片获取成功，进行保存操作
                //1.第一种
                boolean showOriImg = picUrls.isShowOriImg() ;
                Log.d(TAG,"showOriImg-->" + showOriImg ) ;
                String fileName = "img-"+(showOriImg ? "ori-" : "mid-") + picUrls.getUrlId() ;
//                try{
//                    ImageUtils.saveFile(this,bitmap,fileName) ;
//                }catch (IOException e) {
//                    e.printStackTrace();
//                }

                //2.第二种
                int endIndex = fileName.lastIndexOf(".") ;
                String title =  fileName.substring(0,endIndex) ;//要提取.jpg前面的数据，因为下面的title自带jpg格式，避免重复
                String insertImage = MediaStore.Images.Media.
                        insertImage(getContentResolver(),bitmap,title,"zhaopanWB") ;
                ToastUtils.showToast(this,
                        insertImage == null ? "图片保存失败" : "图片保存成功" , Toast.LENGTH_LONG);
                break;
            case R.id.btn_original_image:
                //把标识位设置为true也就是说显示原图
                picUrls.setShowOriImg(true);
                //显示更新
                adapter.notifyDataSetChanged();
                //隐藏原图按钮
                btn_original_image.setVisibility(View.GONE);
                //也要在左右滑动的时候判断，如果当前位置是已经加载过原图，隐藏按钮
                break;
        }
    }
}
